
package com.pp.admin.common.server.service;

import java.util.Iterator;
import java.util.List;
import java.util.logging.Logger;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.pp.admin.common.client.beans.BaseUser;
import com.pp.admin.common.client.beans.ListContents;
import com.pp.admin.common.client.beans.PageInfo;
import com.pp.admin.common.client.exceptions.InvalidDomain;
import com.pp.admin.common.client.service.UserService;
import com.pp.admin.common.server.PersistenceManagerFactory;
import com.pp.admin.common.server.entities.Domain;
import com.pp.admin.common.server.entities.User;
import com.pp.admin.common.server.entities.UsersDomain;

public class UserServiceImpl extends BaseAdminService implements UserService {
	
	/**
	 * 
	 */
	private static final long	serialVersionUID	= 4817651145190319645L;
	
	@SuppressWarnings( "unused" )
	private static final Logger	LOG					= Logger.getLogger( UserServiceImpl.class.getName() );
	
	@SuppressWarnings( "unchecked" )
	@Override
	public ListContents listBaseUsers( PageInfo pageInfo ) {

		ListContents resp = new ListContents();
		PersistenceManager pm = PersistenceManagerFactory.getPersistenceManager();
		try {
			Query query = pm.newQuery( User.class );
			query = setupPageInfo( query, pageInfo );
			
			List<User> userresults = (List<User>) query.execute();
			for ( User user : userresults ) {
				BaseUser baseUser = user.exportBaseUser();
				resp.getContents().add( baseUser );
			}
			resp.setPageInfo( pageInfo );
		} finally {
			PersistenceManagerFactory.closePersistenceManager();
		}
		return resp;
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.pp.admin.client.service.UserService#deleteUsers(java.util.List)
	 */
	@Override
	public void deleteUsers( List<String> listOfIDs ) {

		PersistenceManager pm = PersistenceManagerFactory.getPersistenceManager();
		try {
			Iterator<String> itrUserIDs = listOfIDs.iterator();
			String userID = null;
			while ( itrUserIDs.hasNext() ) {
				userID = itrUserIDs.next();
				
				Key userkey = KeyFactory.createKey( User.class.getSimpleName(), userID );
				User user = pm.getObjectById( User.class, userkey );
				/**
				 * TODO: apply validation to check if the user is owner of any domain/s. If yes then
				 * first corresponding domain/s has to be deleted and then, such user can be
				 * deleted.
				 */
				pm.deletePersistent( user );
				
			}
		} finally {
			PersistenceManagerFactory.closePersistenceManager();
		}
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.pp.admin.client.service.UserService#disableUsers(java.util.List)
	 */
	@Override
	public void disableUsers( List<String> listOfIDs ) {

		setupRequest();
		PersistenceManager pm = PersistenceManagerFactory.getPersistenceManager();
		try {
			Iterator<String> itrUserIDs = listOfIDs.iterator();
			String userID = null;
			while ( itrUserIDs.hasNext() ) {
				userID = itrUserIDs.next();
				User user = User.getUserByUserID( userID );
				user.setStatus( User.STATUS_DELETED );
				
			}
		} finally {
			PersistenceManagerFactory.closePersistenceManager();
		}
		
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.pp.admin.client.service.UserService#save(com.pp.admin.client.beans .User)
	 */
	@Override
	public void save( com.pp.admin.common.client.beans.User user ) {

		setupRequest();
		PersistenceManager pm = PersistenceManagerFactory.getPersistenceManager();
		
		try {
			User entUser = new User().importUser( user );
			entUser.save();
		} finally {
			PersistenceManagerFactory.closePersistenceManager();
		}
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.pp.admin.common.client.service.UserService#listBaseUsersByDomainID
	 * (java.lang.String, com.pp.admin.common.client.beans.PageInfo)
	 */
	@Override
	public ListContents listBaseUsersByDomainID( String domainID, PageInfo pageInfo ) throws InvalidDomain {

		ListContents resp = new ListContents();
		PersistenceManager pm = PersistenceManagerFactory.getPersistenceManager();
		try {
			Domain domain = getDomainEntityByDomainID( domainID );
			Query query = pm.newQuery( UsersDomain.class );
			query.setFilter( FLD_USR_DOMN_DOMAIN + " == " + FLD_USR_DOMN_DOMAIN + QUERY_PARAM );
			query.declareParameters( Domain.class.getName() + " " + FLD_USR_DOMN_DOMAIN + QUERY_PARAM );
			query = setupPageInfo( query, pageInfo, domain );
			List<UsersDomain> usersDomains = (List<UsersDomain>) query.execute( domain );
			for ( UsersDomain usersDomain : usersDomains ) {
				BaseUser baseUser = usersDomain.getUser().exportBaseUser();
				resp.getContents().add( baseUser );
			}
		} finally {
			PersistenceManagerFactory.closePersistenceManager();
		}
		return resp;
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.pp.admin.common.client.service.UserService#activateUsers(java.util .List)
	 */
	@Override
	public void activateUsers( List<String> listOfIDs ) {

		setupRequest();
		PersistenceManager pm = PersistenceManagerFactory.getPersistenceManager();
		try {
			Iterator<String> itrUserIDs = listOfIDs.iterator();
			String userID = null;
			while ( itrUserIDs.hasNext() ) {
				userID = itrUserIDs.next();
				User user = User.getUserByUserID( userID );
				user.setStatus( User.STATUS_ACTIVE );
				
			}
		} finally {
			PersistenceManagerFactory.closePersistenceManager();
		}
		
	}
	
	@Override
	public com.pp.admin.common.client.beans.User getUserByID( String userID ) {

		PersistenceManager pm = PersistenceManagerFactory.getPersistenceManager();
		com.pp.admin.common.client.beans.User userResp = null;
		try {
			userResp = User.getUserByUserID( userID ).exportUser();
		} finally {
			PersistenceManagerFactory.closePersistenceManager();
		}
		return userResp;
	}
	
	@Override
	public void update( com.pp.admin.common.client.beans.User user ) {

		setupRequest();
		PersistenceManager pm = PersistenceManagerFactory.getPersistenceManager();
		
		try {
			User.getUserByUserID( user.getUserID() ).update( user );
		} finally {
			PersistenceManagerFactory.closePersistenceManager();
		}
	}
	
}
